<?php
/**
 * Detail Information of a Contact
 * Display name,gender,type and adress
 *
 * @author Xavier VILAIN
 * @package
 * @license
 */
use Fox\Services;
use Shama\Contact;
use Shama\Messages;
use Fox\Generator;
use Fox\Html\Header\Image,Fox\Html\Button;
use Fox\Html\Wrapper,Fox\Html\TabPanel,Fox\Html\TabPanel\Tab,Fox\Html\Screen;

$xview = Xview::instance();

/* @var $contact Shama\Contact */
$contact = $xview->component('contacts', 'contact');
$data = $contact->getData();
$company = $contact->company();
if(!empty($company))
{
	$data['companyid'] = $company->companyid;
	$data['company'] = $company->company()->name1;
}
$screen = new Screen("sContact","update");
$screen->mainTitle(new Image("contact",Msg(Messages::BUNDLE,Messages::TITLE_CONTACT_UPDATE)));
$screen->toolbar()
		->add(new Button('delete',
						  Generator::display('pDelete',
						  					Generator::url('leads','delete'))))
						  ->color(Button::COLOR_RED);
$blHeader = Xview::instance()->component("contacts", "header");
$screen->add($blHeader);
$blName = Xview::instance()->component("contacts", "name");
$screen->add($blName);
$blAddress = Xview::instance()->component("contacts", "address");
$screen->add($blAddress);
$blAdditonal = Xview::instance()->component("contacts", "additional");
$screen->add($blAdditonal);
$blComment = Xview::instance()->component("contacts", "comment");
$screen->add($blComment);
$screen->optimizeWidth();
$screen->initialize($data);

if($screen->submitted())
{
	$inputs = $screen->get();
	//
	if(!$screen->errors())
	{
		$inputs['contactid'] = $contact->getId();
		try {
			$response = Services::instance()->service(Contact::SERVICE_CREATE)->call($inputs);
			$response = Fox\Utilities::jsonUTF8Decode($response);

			if(isset($response['errors']))
			{
				throw new Exception(implode("<br>",$response['errors']));
			}
			else
			{
				Fox\Message::Success(Msg(Messages::BUNDLE,Messages::MSG_CONTACT_UPDATED,$inputs['name1']." ".$inputs['name2']));
				$screen->redirect();
			}
		}
		catch(Exception $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}
}
echo $screen->Display();